package com.xgpro.common.utils;


import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FileUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import net.coobird.thumbnailator.Thumbnails;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Calendar;


/**
 * @author tx
 * @version 1.0
 * @date 2019/8/23 20:34
 * 文件上传下载工具类
 */
public class FileTool {
    private static  final String UPLOADPATH= ReadPropertiesUtil.readUrl("upload_path");
    /**
     * upLoad 上传文件方法
     * @param file 文件
     * @param filepath 上传文件目录
     * @return 文件地址
     * @throws IllegalStateException
     * @throws IOException
     */
    public static String upLoad(MultipartFile file, String filepath) throws IllegalStateException, IOException {
        //判断是否存在文件
        if(!file.isEmpty()){
            //获取文件名
            String fileName=file.getOriginalFilename();
            //截取文件后缀
            String fileSuffix = fileName.substring(fileName.lastIndexOf('.'), fileName.length()).toLowerCase();
            if(".jpg".equals(fileSuffix) | ".png".equals(fileSuffix) | ".bmp".equals(fileSuffix) | ".jpeg".equals(fileSuffix) |
                    ".JPG".equals(fileSuffix) | ".PNG".equals(fileSuffix) | ".BMP".equals(fileSuffix) | ".JPEG".equals(fileSuffix) |
                    ".pdf".equals(fileSuffix) | ".doc".equals(fileSuffix) | ".docx".equals(fileSuffix) | ".xls".equals(fileSuffix) | ".xlsx".equals(fileSuffix)|".mp4".equals(fileSuffix)|".flv".equals(fileSuffix)|
                    ".wmv".equals(fileSuffix)|".rmvb".equals(fileSuffix)|".3gp".equals(fileSuffix)|".mkv".equals(fileSuffix)|".avi".equals(fileSuffix)){
                Calendar cale = null;
                cale = Calendar.getInstance();
                int year = cale.get(Calendar.YEAR);
                int month = cale.get(Calendar.MONTH) + 1;
                //设置服务器上传路径
                String path = UPLOADPATH + "\\"+filepath+"\\"+year+"\\"+month;
                if (!new File(path).exists()) {
                    new File(path).mkdirs();
                }
                //设置上传文件路径+文件
                String newPath = path+"\\"+fileName;
                System.out.println(newPath);

                if(".jpg".equals(fileSuffix) | ".png".equals(fileSuffix) | ".bmp".equals(fileSuffix) | ".jpeg".equals(fileSuffix) |
                        ".JPG".equals(fileSuffix) | ".PNG".equals(fileSuffix) | ".BMP".equals(fileSuffix) | ".JPEG".equals(fileSuffix)) {
                    Integer width=Integer.parseInt(ReadPropertiesUtil.readUrl("imgWidth"));
                    Thumbnails.of(file.getInputStream()).width(width).toFile(newPath);
                }else {
                    file.transferTo(new File(newPath));
                   // throw new BusinessRuntimeException(ResultCodeEnum.FILE_FAILE_UPLOAD.getCode(),ResultCodeEnum.FILE_FAILE_UPLOAD.getMessage());
                }
                return ReadPropertiesUtil.readUrl("nginxUrl")+filepath+"/"+year+"/"+month+"/"+fileName;
            }else {
                throw new BusinessRuntimeException(ResultCodeEnum.FILE_FAILE_UPLOAD.getCode(),ResultCodeEnum.FILE_FAILE_UPLOAD.getMessage());
            }
        }else {
            throw new BusinessRuntimeException(ResultCodeEnum.UPLOAD_ISEMPTY.getCode(),ResultCodeEnum.UPLOAD_ISEMPTY.getMessage());
        }
    }

    /**
     * download 文件下载方法
     * @param filename 下载文件名称
     * @param filenpath 文件地址
     * @param res
     * @throws IOException
     */
   public  static void download(String filename,String filenpath,HttpServletResponse res) throws IOException {
        res.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
        ServletOutputStream os = res.getOutputStream();
        File flie = new File(filenpath);
        os.write(FileUtils.readFileToByteArray(flie));
        os.flush();
        os.close();
    }
}
